package lumis.content.webresource;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import lumis.content.ContentManagerFactory;
import lumis.content.core.Content;
import lumis.content.core.ContentLocale;
import lumis.content.core.ContentVersion;
import lumis.content.table.ContentTableSource;
import lumis.doui.source.ISourceData;
import lumis.doui.source.SourceContext;
import lumis.doui.source.SourceFactory;
import lumis.doui.table.field.ITableSourceField;
import lumis.portal.PortalException;
import lumis.portal.PortalObjectNotFoundException;
import lumis.portal.PortalRequestParameters;
import lumis.portal.authentication.SessionConfig;
import lumis.portal.manager.ManagerFactory;
import lumis.portal.page.PageWebResource;
import lumis.portal.page.webresource.DefaultPageWebResourceDataProvider;
import lumis.portal.stability.StableMinor;
import lumis.portal.transaction.ITransaction;
import lumis.portal.transaction.PortalTransactionFactory;

/**
 * Provides the page information based on content being rendered.
 * <p>
 * This data provider detects if the current page contains a content id as
 * a parameter.  If true, it provides the content title and introduction from
 * the ContentManager.
 *
 * @version $Revision: 13082 $ $Date: 2011-05-27 16:03:12 -0300 (Fri, 27 May 2011) $
 * @since 5.0.0
 */
@StableMinor(version = "6.0", sinceVersion = "5.0")
public class ContentPageWebResourceDataProvider extends DefaultPageWebResourceDataProvider
{
	private String title = null;
	private String description = null;
	private List<String> contentKeywords = Collections.emptyList();
	
	public void init(PageWebResource instance) throws PortalException
	{
		super.init(instance);
		
		try
		{
			Content content = null;
			ContentVersion contentVersion = null;
			
			if(pageWebResource.isPreviewModeEnabled())
			{
				String itemId = pageWebResource.getParameter(PortalRequestParameters.PAGE_PARAMETER_PREVIEW_ITEMID);
				if(itemId != null)
					contentVersion  = ContentManagerFactory.getContentManager().getContentVersionByItemId(itemId);
			}
			
			if(contentVersion == null)
			{
				String contentId = pageWebResource.getParameter(PortalRequestParameters.PAGE_PARAMETER_ITEM_ID);
				if(contentId != null)
					content = ContentManagerFactory.getContentManager().getContent(contentId);
				else
				{
					String itemId = pageWebResource.getParameter(PortalRequestParameters.PAGE_PARAMETER_ITEMID);
					if(itemId != null)
						content  = ContentManagerFactory.getContentManager().getContentByItemId(itemId);
				}
				
				if(content != null)
				{
					if(!pageWebResource.isPreviewModeEnabled())
					{
						contentVersion = ContentManagerFactory.getContentManager().getPublishedContentVersion(content.getId(), content.getServiceInstanceId());
					}
					else
					{
						ContentLocale contentLocale = content.getContentLocale(pageWebResource.getLocale(), true);
						if(contentLocale != null)
							contentVersion = contentLocale.getActiveVersion();
					}
				}
			}
			
			if(contentVersion != null)
			{
				Content curContent = contentVersion.getContentLocale().getContent();
				String sourceId = curContent.getSourceId();
				String serviceInstanceId = curContent.getServiceInstanceId();
				
				ITransaction currentTransaction = PortalTransactionFactory.getCurrentTransaction();
				SourceContext sourceContext = new SourceContext(SessionConfig.getCurrentSessionConfig(), ManagerFactory.getServiceInstanceManager().get(serviceInstanceId, currentTransaction), currentTransaction);
				ContentTableSource contentTableSource = (ContentTableSource)SourceFactory.createSource(sourceId, sourceContext);
				String primaryKeyValue = contentVersion.getItemId();
				ISourceData data = contentTableSource.readData(primaryKeyValue, currentTransaction);
				
				ITableSourceField primaryNameField = contentTableSource.getPrimaryNameField();
				if(primaryNameField != null)
					title = data.get(primaryNameField.getId(), String.class);
				else
					title = contentVersion.getPrimaryName();
				
				ITableSourceField introductionField = contentTableSource.getIntroductionField();
				if(introductionField != null)
					description = data.get(introductionField.getId(), String.class);
				else
					description = contentVersion.getIntroduction();
				
				ITableSourceField keywordsField = contentTableSource.getKeywordsField();
				if(keywordsField != null)
				{
					String keywordsStr = data.get(keywordsField.getId(), String.class);
					if(keywordsStr != null)
						contentKeywords = Arrays.asList(keywordsStr.split(","));
				};
			}
		}
		catch(PortalObjectNotFoundException e)
		{
		}
	}

	@Override
	protected String getDescription() throws PortalException
	{
		if (description != null)
			return description;
		else
			return super.getDescription();
	}

	@Override
	protected List<String> getKeywords() throws PortalException
	{
		List<String> pageKeywords = super.getKeywords();
		List<String> result = new ArrayList<String>(pageKeywords.size() + contentKeywords.size());
		result.addAll(pageKeywords);
		result.addAll(contentKeywords);
		return result;
	}

	@Override
	protected String getTitle() throws PortalException
	{
		if (title != null)
			return title;
		else
			return super.getTitle();
	}
}